﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Validation;

namespace JobbJakt
{
    class JobbJaktDB
    {
        private static readonly JJModelContainer _context = new JJModelContainer();

        public static JJModelContainer Context
        {
            get { return _context; }
        }

        private static readonly DbSet<Education> _educations = Context.EducationSet;
        private static readonly DbSet<Advertisement> _advertisements = Context.AdvertisementSet;
        private static readonly DbSet<CompanyContact> _companyContacts = Context.CompanyContactSet;
        private static readonly DbSet<Users> _users = Context.UsersSet;
        private static readonly DbSet<JobExperience> _jobExperiences = Context.JobExperienceSet;
        private static readonly DbSet<Merits> _merits = Context.MeritsSet;
        private static readonly DbSet<Language> _languages = Context.LanguageSet;
        private static readonly DbSet<ErrorTable> _errors = Context.ErrorTableSet;
        private static readonly DbSet<Applications> _applications = Context.ApplicationsSet;

        public static DbSet<Education> Educations
        {
            get { return _educations; }
        }
        public static DbSet<Advertisement> Advertisements
        {
            get { return _advertisements; }
        }
        public static DbSet<CompanyContact> CompanyContacts
        {
            get { return _companyContacts; }
        }
        public static DbSet<Users> Userses
        {
            get { return _users; }
        }
        public static DbSet<JobExperience> JobExperiences
        {
            get { return _jobExperiences; }
        }
        public static DbSet<Merits> Meritses
        {
            get { return _merits; }
        }
        public static DbSet<Language> Languages
        {
            get { return _languages; }
        }
        public static DbSet<ErrorTable> ErrorTables
        {
            get { return _errors; }
        }
        public static DbSet<Applications> Applications
        {
            get { return _applications; }
        }
        public static int Save()
        {
            try
            {
                return Context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }

    }
}
